#include <g2d.h>

class MyGame : public g2d::Game {
  public:
    MyGame() : fullscreen_(0) {}
  protected:
    virtual g2d::Result load_resource(g2d::Font &f, g2d::Texture &t, g2d::Audio &a) override {
      auto rst = t.load_file("back", "empty/back.jpg");
      if (g2d::Succeeded(rst))
        rst = f.load("font", "fonts/simfang.ttf", 20);
      if (g2d::Succeeded(rst))
        rst = a.load_music_file("music", "audio/music.mp3");
      if (g2d::Succeeded(rst))
        rst = a.load_sfx_file("sfx", "audio/sfx.mp3");
      return rst;
    }
    virtual void unload_resource(g2d::Font &f, g2d::Texture &t, g2d::Audio &a) override {
      t.unload("back");
      f.unload("font");
      a.unload_music("music");
      a.unload_sfx("sfx");
    }
    void update(g2d::Input &inp, g2d::Audio &aud) override {
      // esc
      if (inp.key_pressed(SDL_SCANCODE_ESCAPE)) {
        close_window();
      }

      // f
      if (inp.key_pressed(SDL_SCANCODE_F)) {
        if (fps() == 0) set_fps(60);
        else set_fps(0);
      }

      // w
      if (inp.key_pressed(SDL_SCANCODE_W)) {
        ++fullscreen_;
        if (fullscreen_ > 2)
          fullscreen_ = 0;
        switch (fullscreen_) {
          case 0: exit_fullscreen(); break;
          case 1: enter_fullscreen(false); break;
          case 2: enter_fullscreen(true); break;
        }
        aud.play_sfx("sfx");
      }

      // m
      if (inp.key_pressed(SDL_SCANCODE_M)) {
        aud.mute(!aud.muted());
      }
    }
    virtual void render(g2d::Renderer &r) override {
      r.draw_texture("back", 0);
      draw_info(r);
    }
  protected:
    void draw_info(g2d::Renderer &r) {
      const char *font = "font";

      std::string text = "FPS: ";
      text += std::to_string(real_fps());

      SDL_Color color;
      color.a = 255; color.r = 0; color.g = 0; color.b = 0;

      int W, H, w, h;
      window_size(&W, &H);
      r.text_size(font, text.c_str(), &w, &h);

      r.draw_text(font, text.c_str(), color, W-w-5, 2);

      text = "Window Size: ";
      text += std::to_string(W);
      text += ",";
      text += std::to_string(H);
      r.text_size(font, text.c_str(), &w, &h);
      r.draw_text(font, text.c_str(), color, W-w-5, h*1+2);

      text = "Screen: ";
      switch (fullscreen_) {
        case 0: text += "windowed"; break;
        case 1: text += "fake fullscreen"; break;
        case 2: text += "real fullscreen"; break;
      }
      r.text_size(font, text.c_str(), &w, &h);
      r.draw_text(font, text.c_str(), color, W-w-5, h*2+2);

      r.draw_text(font, "Press key ESC to QUIT", color, 0, h*0+2);
      r.draw_text(font, "Press key F   to Toggle FPS", color, 0, h*1+2);
      r.draw_text(font, "Press key W   to Toggle Screen Mode", color, 0, h*2+2);
      r.draw_text(font, "Press key M   to Toggle Sound", color, 0, h*3+2);
    }
  protected:
    int fullscreen_;
};

int main(int argc, char *argv[]) {
  Uint32 fps = 60;
  MyGame game;
  auto rst = game.init("Game2D Empty", 650, 468);
  if (g2d::Succeeded(rst)) {
    game.audio().play_music("music", -1);
    game.set_fps(fps);
    game.run();
  }
  game.uninit();
  return 0;
}